package com.dubbo.CarBalance.service;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.CarBalance.AccountAccessoriesService;
import com.dubbo.CarBalance.mapper.AccountAccessoriesMapper;
import com.dubbo.CarBalance.mapper.AccountDeatilMapper;
import com.dubbo.CarBalance.utils.PageUtils;
import com.dubbo.common.dtos.AppHttpCodeEnum;
import com.dubbo.common.dtos.ResponseResult;
import com.dubbo.common.exception.CarServiceException;
import com.dubbo.model.balance.dtos.AccountAccessPayResultDto;
import com.dubbo.model.balance.dtos.AccountAccessoriesDto;
import com.dubbo.model.balance.dtos.AccountAccessoriesPayDto;
import com.dubbo.model.balance.dtos.AccountDetailDto;
import com.dubbo.model.balance.pojos.AccountAccessories;
import com.dubbo.model.balance.pojos.AccountCarer;
import com.dubbo.model.balance.pojos.AccountDetail;
import com.dubbo.utils.common.BeanHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author harbour*/



@Service
@Component
public class AccountAccessoriesServiceImpl implements AccountAccessoriesService {

    @Autowired
    private AccountAccessoriesMapper mapper;

    @Autowired
    private AccountDeatilMapper deatilMapper;

    @Override
    public ResponseResult all(AccountAccessoriesDto dto) {
        dto.checkParam();
        return PageUtils.getPageResult(dto,getPageInfo(dto));
    }

    private PageInfo<AccountAccessories> getPageInfo(AccountAccessoriesDto dto){
        int page = dto.getPage();
        int size = dto.getSize();
        PageHelper.startPage(page,size);
        List<AccountAccessories> accountAccessories = mapper.selectPage(dto);
        return new PageInfo<AccountAccessories>(accountAccessories);
    }

    @Override
    public ResponseResult<AccountDetailDto> detail(String id) {
        //参数校验
        if (id == null){
            throw new CarServiceException(AppHttpCodeEnum.PARAM_INVALID);
        }

        AccountDetailDto detailDto = new AccountDetailDto();

        //查询配件商账单明细
        AccountAccessories accessories = mapper.findById(id);
        if (accessories!=null){
            detailDto.setAccountId(accessories.getAccountId());
            detailDto.setDigest(accessories.getDigest());
            detailDto.setPrice(accessories.getPrice());
            detailDto.setRemark(accessories.getRemark());
        }

        //查询配件商账单详情
        AccountDetail accountDetail = deatilMapper.findbyAccountId(id);
        if (accountDetail!=null){
            detailDto.setAccountDetail(accountDetail);
        }

        return ResponseResult.okResult(detailDto);
    }


    @Override
    public ResponseResult<List<AccountAccessPayResultDto>> account(AccountAccessoriesPayDto dto) {
        if (dto==null){
            throw new CarServiceException(AppHttpCodeEnum.PARAM_INVALID);
        }

        List<AccountAccessories> accountAccessoriesList = mapper.findByCustomerId(dto);
        if (CollectionUtils.isNotEmpty(accountAccessoriesList)){
            List<AccountAccessPayResultDto> payResultDtos = BeanHelper.copyWithCollection(accountAccessoriesList, AccountAccessPayResultDto.class);
            return ResponseResult.okResult(payResultDtos);
        }

        return ResponseResult.okResult(null);
    }
}
